package com.asking.controller;

import com.asking.domain.User;
import com.asking.service.UserService;
import com.asking.utils.GetRandomCodeUtil;
import com.asking.utils.SmsUtil;
import com.asking.vo.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 用户注册
 * @author 蔺思卓
 */
@RestController
@RequestMapping("/regist")
public class RegistController {

    @Resource
    private UserService userService;
    @Resource
    private HttpSession session;

    /**
     * 发送短信验证码
     * @param phone
     * @return
     */
    @GetMapping("/{phone}")
    public ResponseEntity<Result> phoneCode(@PathVariable("phone") String phone) {
        //随机验证码
        String code = GetRandomCodeUtil.getCode();
        //将验证码保留   (保留到session)
        System.out.println(code);
        session.setAttribute(phone, code);
        //发送短信
        try {
            //    SmsUtil.sendSms(phone, code);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ResponseEntity<>(new Result(200, "null"), HttpStatus.OK);
    }

    /**
     * 用户注册
     * @param checkcode
     * @param user
     * @return
     */
    @PostMapping()
    public ResponseEntity<Result> regist(String checkcode, User user) {
        String code;
        try{
            //获得从session中获得通过手机号保存的验证码
             code = session.getAttribute(user.getPhone()).toString();
        }catch (Exception e){
            return new ResponseEntity<>(new Result(500, "未找到验证码,请重新发送"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        if ( !"".equals(code.trim())) {

            if (code.equals(checkcode)) {
                try {
                    Result add = userService.add(user);
                    return new ResponseEntity<>(add, HttpStatus.CREATED);
                } catch (Exception e) {
                    return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
                }
            }
            return new ResponseEntity<>(new Result(500, "验证码不对"), HttpStatus.INTERNAL_SERVER_ERROR);

        }else {
            return new ResponseEntity<>(new Result(500, "未找到验证码,请重新发送"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 忘记密码时检测短息与验证码是否一致
     * @param checkcode
     * @param phone
     * @return
     */
    @PostMapping("/check")
    public ResponseEntity<Result> checkcode(String checkcode,String phone){
        String code="";
        try{
            code = session.getAttribute(phone).toString();
        }catch (Exception e){
            return new ResponseEntity<>(new Result(500, "未找到验证码,请重新发送"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
        if ( !"".equals(code.trim())) {
            if (code.equals(checkcode)){
                return new ResponseEntity<>(new Result(200,""),HttpStatus.OK);
            }
        }
        return new ResponseEntity<>(new Result(500, "未找到验证码,请重新发送"), HttpStatus.INTERNAL_SERVER_ERROR);

    }

    /**
     * 忘记密码后的重置密码
     * @param phone
     * @param password
     * @return
     */
    @PostMapping("reset/password")
    public  ResponseEntity<Result> reset(String phone,String password){
        try{
            User user = userService.selectByPhone(phone);
            user.setPassword(password);
            Result result = userService.updata(user);
            return new ResponseEntity<>(result,HttpStatus.NO_CONTENT);
        }catch (Exception e){
            return new ResponseEntity<>(new Result(500,"服务器异常,请联系管理员"),HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}

